<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Jobs\ProcessPodcast;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;

use App\Contracts\PaymentGateway;//依赖注入使用测试契约

use Illuminate\Support\Facades\Mail;
use App\Mail\QQMail;

use Illuminate\Support\Facades\View;


class homeController extends Controller
{
    protected $paymentGateway;

    public function __construct(PaymentGateway $paymentGateway)
    {
        $this->paymentGateway = $paymentGateway;
    }

    public function pay()
    {
        $this->paymentGateway->charge(100);
        return $this->ajaxReturn(['code' => 1, 'msg' => '契约实现的测试完成！']);
    }

    //redis队列的异步任务处理示例
    public function missiondispatch(Request $request)
    {
        //sleep(5);
        $podcast = [
            'test1' => '这是测试1的信息',
            'test2' => '这是测试2的信息',
            'test3' => '这是测试3的信息',
        ];
        if (Cache::has("lockmissiondispatch")) {
            return $this->ajaxReturn(['code' => 0, 'msg' => '另一个进程正在执行任务,你不能重复执行']);
        }
        // 分发任务到队列中
        //$delayInSeconds = 10; // 设置延迟时间为10秒后执行
        //ProcessPodcast::dispatch($podcast);
        //->delay(now()->addSeconds($delayInSeconds));
        //发送QQ邮箱
        $this->sendQQMail();
        return $this->ajaxReturn(['code' => 1, 'msg' => '操作成功！']);
    }

    public function sendQQMail()
    {
        $toEmail = '1793026122@qq.com'; // 收件人邮箱
        $subject = 'laravel测试邮件标题';
        $content = '这是laravel测试邮件的内容。';
        // 发送邮件
        Mail::to($toEmail)->send(new QQMail($subject, $content));
        return '邮件发送成功！';
    }
}